﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JTicket.Common;
using JTicket.BLL;
namespace JTicket.UI
{
    public partial class JT_BoardAdd : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Flag"] != null)
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["Flag"].ToString() == "Edite")
                    {
                        Bind();
                        this.BtnAdd.Visible = false;
                        this.BtnEdite.Visible = true;

                    }
                    this.txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
        }
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            //添加公告
            JTicket.Model.D_Boardinfo boardModel = new Model.D_Boardinfo();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            boardModel.note = "禁用";
            if (this.chState.Checked)
            {
                boardModel.note = "启用";
            }
            boardModel.sContent = this.TxtContent.Text;
            boardModel.sdate = this.txtTime.Text.Substring(0, 10);
            boardModel.Stime = this.txtTime.Text.Substring(11, 8);
            boardModel.sTitle = this.TxtTitle.Text;
            boardModel.ThCode = bllOper.GetModel(User.Identity.Name).ThCode;
            BLL.D_Boardinfo.Add(boardModel); 
            Response.Redirect("JT_BoardManager.aspx");
        }
        protected void BtnEdite_Click(object sender, EventArgs e)
        {
            //修改公告
            JTicket.Model.D_Boardinfo boardModel = new Model.D_Boardinfo();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            boardModel.note = "禁用";
            if (this.chState.Checked)
            {
                boardModel.note = "启用";
            }
            boardModel.id = int.Parse(Request.QueryString["ID"]);
            boardModel.sContent = this.TxtContent.Text;
            boardModel.sdate = this.txtTime.Text.Substring(0, 10);
            boardModel.Stime = this.txtTime.Text.Substring(11, 8);
            boardModel.sTitle = this.TxtTitle.Text;
            boardModel.ThCode = bllOper.GetModel(User.Identity.Name).ThCode;
            BLL.D_Boardinfo.Update(boardModel);
            //Bind();
            JsMsg.Alert("修改成功！");
            
        }
        void Bind()
        {
            if (Request.QueryString["ID"]!=null)
            {
                BLL.D_Boardinfo bllBoard = new BLL.D_Boardinfo();
                JTicket.Model.D_Boardinfo board = bllBoard.GetModel(int.Parse(Request.QueryString["ID"]));

                this.TxtContent.Text = board.sContent;
                this.TxtTitle.Text = board.sTitle;
                this.txtTime.Text = string.Format("{0} {1}", board.sdate, board.Stime);
                if (board.note.Equals("启用"))
                    this.chState.Checked = true;
                else
                    this.chState.Checked = false;
            }
        }

        protected void BtnEdite0_Click(object sender, EventArgs e)
        {
            Response.Redirect("JT_BoardManager.aspx");
        }
    }
}